package com.frogs.ucenter.service.daoimpl.aop;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.frogs.component.permission.api.service.PermissionHelper;
import com.frogs.component.permission.entity.User;
import com.frogs.system.util.ChangeTypeEnum;
import com.frogs.ucenter.dao.SubFlowTaskRelationDao;
import com.frogs.ucenter.dao.SubFlowTaskRelationLogDao;
import com.frogs.ucenter.entity.SubFlowTaskRelation;
import com.frogs.ucenter.entity.SubFlowTaskRelationLog;

@Aspect
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service("subFlowTaskRelationLogAopImpl")
public class SubFlowTaskRelationLogAopImpl {
	@Resource
	private SubFlowTaskRelationLogDao subFlowTaskRelationLogDao;
	@Resource
	private SubFlowTaskRelationDao subFlowTaskRelationDao;
	@Resource(name = "permissionHelper")
	private PermissionHelper permissionHelper;
	
	private static final Logger log = LoggerFactory.getLogger(TaskInfoLogAopImpl.class);

	
	@AfterReturning(pointcut="execution(* com.frogs.ucenter.controller.TaskInfoController.addSubFlowInfo(..)) || "
			+ "execution(* com.frogs.ucenter.controller.TaskInfoController.deleteRelation(..))")
	public void doAfterReturning(JoinPoint jp)
	{
		try {
			HttpServletRequest req = (HttpServletRequest)jp.getArgs()[0];//切入点方法入参
			boolean flag = (Boolean) req.getAttribute("flag");
			String method = jp.getStaticPart().getSignature().getName();
			if(!flag){
				return;
			}
			SubFlowTaskRelation subFlowTaskRelation = new SubFlowTaskRelation();
			int changeType = 0;
			User user = permissionHelper.getUser();
			int userId = user.getUserId();
			if(method.equals("addSubFlowInfo")){
				subFlowTaskRelation = (SubFlowTaskRelation)jp.getArgs()[3];
				changeType = ChangeTypeEnum.ADD.getId();
				insertLog(subFlowTaskRelation, changeType, userId);
			}else if(method.equals("deleteRelation")){
				String deleteId = jp.getArgs()[3].toString();
				String[] arr = deleteId.split(",");
				changeType = ChangeTypeEnum.DELETE.getId();
				for(int i=0;i<arr.length;i++){
					subFlowTaskRelation = new SubFlowTaskRelation();
					subFlowTaskRelation.setId(Integer.parseInt(arr[i]));
					insertLog(subFlowTaskRelation, changeType, userId);
				}
			}
		} catch (Exception e) {
			log.error("func[doAfterReturning] jp[" + jp + "] error[" + e.getMessage() + "]", e);
		}
		
	}
	
	@Async
	private void insertLog(SubFlowTaskRelation subFlowTaskRelation, int changeType, int creater){
		SubFlowTaskRelationLog subFlowTaskRelationLog = new SubFlowTaskRelationLog();
		subFlowTaskRelationLog.setChangeType(changeType);
		subFlowTaskRelationLog.setCreater(creater);
		subFlowTaskRelationLog.setOldId(subFlowTaskRelation.getId());
		subFlowTaskRelationLog.setSequence(subFlowTaskRelation.getSequence());
		subFlowTaskRelationLog.setSubFlowId(subFlowTaskRelation.getSubFlowId());
		subFlowTaskRelationLog.setTaskId(subFlowTaskRelation.getTaskId());
		subFlowTaskRelationLogDao.insert(subFlowTaskRelationLog);
	}
}
